package com.xiaomaoguai.webkit.web.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes({ "ctx", "language" })
public class IndexAction {

	public static final String LOGIN_USER_SESSION_KEY = "user";

	@RequestMapping(value = "/index.xhtml", method = { RequestMethod.GET })
	public String index(HttpServletRequest request, ModelMap model) throws Exception {
		model.put("ctx", request.getContextPath());
		Locale defaultLocale = Locale.getDefault();
		String country = defaultLocale.getCountry();// 返回国家地区代码
		String language = defaultLocale.getLanguage();// 返回国家的语言
		model.put("language", language + "_" + country);
		return "/index";
	}

	@RequestMapping(value = "/login.xhtml", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView login(ModelAndView view, HttpServletRequest request) throws Exception {

		String account = request.getParameter("user.account");
		String password = request.getParameter("user.password");

		System.out.println(account);

		System.out.println(password);

		view.setViewName("redirect:/index.xhtml");

		return view;
	}

	@RequestMapping(value = "/gotoLogin.html", method = { RequestMethod.GET })
	public String gotoLogin(ModelMap modeMap, HttpServletRequest requet) {
		Locale defaultLocale = Locale.getDefault();
		String country = defaultLocale.getCountry();// 返回国家地区代码
		String language = defaultLocale.getLanguage();// 返回国家的语言
		requet.getSession().getServletContext().setAttribute("language", language + "_" + country);
		return "/login";
	}

	@RequestMapping(value = "/logout.html", method = { RequestMethod.GET })
	public ModelAndView logout(ModelAndView view, HttpServletRequest request) {
		request.getSession().invalidate();
		view.setViewName("redirect:/gotoLogin");
		return view;
	}

	@RequestMapping(value = "/west.html", method = { RequestMethod.GET })
	public String west(ModelMap model) {
		// List<Module> modules = null;
		// try {
		// modules = securityService.loadSystemNavigation();
		// ModuleUtils.filter(modules,
		// SecurityThreadContext.getPrincipalToken().getResources(),
		// SecurityConstants.SECURITY_DISPLAY_NAVIGATOR,
		// SecurityConstants.RESOURCE_PROTOCOL_HTTP,
		// SecurityConstants.RESOURCE_PHASE_PROTAL);
		// } catch (Exception e) {
		// logger.warn("load navigation failure 请联系管理员 检查访问权限...");
		// }
		// Collections.sort(modules, new Comparator<Module>() {
		//
		// @Override
		// public int compare(Module item1, Module item2) {
		// return item1.getId() > item2.getId() ? 0 : 1;
		// }
		// });
		// model.put("myModules", modules);

		return "/layout/west";
	}

	@RequestMapping(value = "/north.html")
	public String north() {
		return "/layout/north";
	}

	@RequestMapping(value = "/east.html", method = { RequestMethod.GET })
	public String east() {
		return "/layout/east";
	}

	@RequestMapping(value = "/south.html", method = { RequestMethod.GET })
	public String south() {
		return "/layout/south";
	}

	@RequestMapping(value = "/center.html", method = { RequestMethod.GET })
	public String center() {
		return "/layout/center";
	}

	@RequestMapping(value = "/include.html", method = { RequestMethod.GET })
	public String include() {
		return "/include";
	}
}
